﻿using B.S.Outsourcing.Domain;
using B.S.Outsourcing.ErrorCode;
using B.S.Outsourcing.Infatructrue;
using B.S.Outsourcing.Writer.Api.Application.Command;
using MediatR;

namespace B.S.Outsourcing.Writer.Api.Application.CommandHandler
{
    public class UpdateOutsourCommandHandler : IRequestHandler<UpdateOutsourCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<OutsourcingModel> baseRepository;
        private readonly DBHelper1 dBHelper;

        public UpdateOutsourCommandHandler(IBaseRepository<OutsourcingModel> baseRepository, DBHelper1 dBHelper)
        {
            this.baseRepository = baseRepository;
            this.dBHelper = dBHelper;
        }

        public async Task<ApiResult<int>> Handle(UpdateOutsourCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var sql = $"update OutsourcingModel set Name='{request.Name}',Abbreviation='{request.Abbreviation}',Status='{request.Status}',Address='{request.Address}',PostalCode='{request.PostalCode}',Telephone='{request.Telephone}',Remarks='{request.Remarks}',ContactPerson='{request.ContactPerson}',Mobile='{request.Mobile}',Bank='{request.Bank}',BankAddressPhone='{request.BankAddressPhone}',AccountNumber='{request.AccountNumber}',TaxId='{request.TaxId}',IsDelete='{request.IsDelete}' where Id='{request.Id}'";
            var str = dBHelper.NonQuery(sql);
            if (str > 0)
            {
                result.Code = ApiEnums.成功;
                result.Message = "成功";
                result.Data = str;
            }
            else
            {
                result.Code = ApiEnums.失败;
                result.Message = "失败";
                result.Data = str;
            }
            return await Task.FromResult(result);
        }
    }
}
